package com.zr.userrelation.controller;

import com.zr.meiju.GuanXiEnum;
import com.zr.useredu.pojo.XiaLaVo;
import com.zr.userrelation.pojo.UserRelationAddVo;
import com.zr.userrelation.service.UserRelationService;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 联系人的接口
 * Created by star laon 2019/7/3.
 */
@RestController
public class UserRelationController {
    @Autowired
    private UserRelationService userRelationService;

    /**
     * 根据Loginid进行查询
     */
    @GetMapping("queryLoginId")
    public ResultVo queryLoginId(HttpServletRequest request){
        Integer loginId = (Integer)request.getSession().getAttribute("loginId");
        System.out.println(loginId);
        return userRelationService.queryLoginId(loginId);
    }
    /**
     * 和我关系的下拉框
     */
    @GetMapping("withguanxiDropdownBox")
    public ResultVo<List<XiaLaVo>> withguanxiDropdownBox(){

        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (int i = 0; i< GuanXiEnum.values().length; i++){
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setId(i);
            xiaLaVo.setName(GuanXiEnum.getName(i));
            xiaLaVoList.add(xiaLaVo);
        }
        return ResultBuildVo.success(xiaLaVoList);
    }

    /**
     * 添加教育情况
     */
    @PostMapping("addContacts")
    public ResultVo addContacts(@RequestBody @Valid UserRelationAddVo userRelationAddVo, BindingResult bindingResult,HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(), "500");
        }
        return userRelationService.addContacts(userRelationAddVo,request);
    }
}
